/**
 * Created by zhaolong on 2018/7/7.
 */
let map = null;
let animation = null;
let flag = 0;

export function initOdBusAnimation(_map) {
  map = _map;
  animation = requestAnimationFrame(fnOBA);
}

function fnOBA() {
  if (map && map.isStyleLoaded()) {
    if (map.getLayer('top-stops-circle') || map.getLayer('normal-stops-circle') || map.getLayer('bottom-stops-circle')) {
      changeProp('top-stops-circle', 'circle-stroke-width', 0, 18);
      changeProp('normal-stops-circle', 'circle-stroke-width', 0, 8);
      changeProp('bottom-stops-circle', 'circle-stroke-width', 0, 16);
    }
  }
  animation = requestAnimationFrame(fnOBA);
}

function changeProp(layerid, prop, min, max) {
  let step = max - min;
  let oldVal = map.getPaintProperty(layerid, prop);
  if (oldVal == min) flag = 1;
  if (oldVal == max) flag = -1;
  let newVal = oldVal + (2 * (step / 100) * flag);
  newVal = (newVal < 0) ? 0 : Number(newVal.toFixed(2));
  map.setPaintProperty(layerid, prop, newVal);
}

export function clearOdBusAnimation() {
  cancelAnimationFrame(animation);
}
